package com.TicTacToe;

import java.util.Random;

import android.view.View.OnClickListener;

public class TicTacToeGame {
	private static char Board[];  // declaring the board
	private final static int Board_Size = 9;  // declaring the size of the board
	public static final char Human = 'X';  // the value of the human
	public static final char  Computer = 'O';  // the value of the Computer
	public static final char  Human1 = 'X'; // the value of the human 1
	public static final char  Human2 = 'O'; // the value of the human 2
	public static final char Empty = ' ';  //  the value of the empty buttons
	private Random gRand; // for the AI so it can play
	
	public static int getBoard_Size(){
		
		//so we can set the buttons at the activity
		
		return  Board_Size; 
	}
	public TicTacToeGame(){
		
		// making the board with the size
		
		Board = new char [Board_Size];
		for(int i =0;i < Board_Size;i++ ){
			Board[i]=Empty;
		}
		
		
		gRand = new Random();
		
	}
	
	public void clearBoard(){
		
		// clearing the board from any previous actions 
		
		for(int i=0;i < Board_Size;i++){
			Board[i]=Empty;
		}		
	}
	
	public void setMove(char player,int location){
		
		// assigning the values to the player
		
		Board[location]=player;
	}
	
	public void setMove2(char player,int location){
		Board[location]=player;
	}
	
	public static void waiting (int n){
        
        long t0, t1;

        t0 =  System.currentTimeMillis();

        do{
            t1 = System.currentTimeMillis();
        }
        while (t1 - t0 < 10000);
    }
	
	public int getComputerMove(){
		
		// making the AI move
	
		int move;
		
		for(int i=0;i < Board_Size;i++ ){
			
			// if the board is an empty location
			
			if(Board[i] != Human && Board[i] != Computer){
				
				// get the empty locations
				
				char current = Board[i];
				Board[i] = Computer;
				
				//checking for a winner to make the AI stop
				
				if(checkForWinnerAi() == 3){
					
					setMove(Computer,i);
					return i;
				}else
					Board[i] = current;
			}
		}
		
		for(int i=0 ; i < getBoard_Size();i++){
			
			if(Board[i] != Human && Board[i] != Computer){
				char current = Board[i];
				Board[i] = Human;
				
				if(checkForWinnerAi() == 2){
					setMove(Computer,i);
					return i;
					
				}else
					Board[i] = current;
			}
		}
		
		do	{			
			
			// moving the AI randomly
			
			move = gRand.nextInt(getBoard_Size());
			
		}while(Board[move] == Human || Board[move] == Computer );;
			setMove(Computer, move);
			 
			return move;
			
			
	}
	
	// the rules of winning
	
	public static int checkForWinnerAi(){
		
		// columns
		
		for(int i=0;i <=6;i+=3){
			// if the human wins
			
			if(Board[i] == Human &&
				Board[i+1] == Human &&
				Board[i+2] == Human){
				return 2;
			}
			// if the computer wins
			
			if(Board[i] == Computer &&
					Board[i+1] == Computer &&
					Board[i+2] == Computer){
					return 3;
		}
		}	
		// rows
		
		for(int i=0;i<=2;i++){
			
			// if the human wins
			
			if(Board[i] == Human &&
				Board[i+3] == Human &&
				Board[i+6] == Human){
				return 2;
			}
			// if the computer wins
			
			if(Board[i] == Computer &&
					Board[i+3] == Computer &&
					Board[i+6] == Computer){
					return 3;
		}
		}
		// from the left diagonaly
		
		// if the human wins	
		
		if((Board[0] == Human &&
			Board[4] == Human &&
			Board[8] == Human) ||
			Board[2] == Human &&
			Board[4] == Human &&
			Board[6] == Human){
			return 2;
		}
		
		// from the right diagonaly
		
		// if the computer wins
		
		if((Board[0] == Computer &&
				Board[4] == Computer &&
				Board[8] == Computer) ||
				Board[2] == Computer &&
				Board[4] == Computer &&
				Board[6] == Computer){
				return 3;
			
		}
	
		// if its a draw
		
		for(int i=0;i<getBoard_Size();i++){
			if(Board[i] != Human && Board[i] != Computer){
				return 0;
			}
		}
		
		// if nothing of the above happens continue the game
		
		return 1;
	}
	
	
	// the rules of winning
	
	public static int checkForWinner2player(){
		
		// columns
		
		for(int i=0;i <=6;i+=3){
			// if the human wins
			
			if(Board[i] == Human1 &&
				Board[i+1] == Human1 &&
				Board[i+2] == Human1){
				return 2;
			}
			// if the computer wins
			
			if(Board[i] == Human2 &&
					Board[i+1] == Human2 &&
					Board[i+2] == Human2){
					return 3;
		}
		}	
		// rows
		
		for(int i=0;i<=2;i++){
			
			// if the human wins
			
			if(Board[i] == Human1 &&
				Board[i+3] == Human1 &&
				Board[i+6] == Human1){
				return 2;
			}
			// if the computer wins
			
			if(Board[i] == Human2 &&
					Board[i+3] == Human2 &&
					Board[i+6] == Human2){
					return 3;
		}
		}
		// from the left diagonaly
		
		// if the human 1 wins	
		
		if((Board[0] == Human1 &&
			Board[4] == Human1 &&
			Board[8] == Human1) ||
			Board[2] == Human1 &&
			Board[4] == Human1 &&
			Board[6] == Human1){
			return 2;
		}
		
		// from the right diagonaly
		
		// if the Human 2 wins
		
		if((Board[0] == Human2 &&
				Board[4] == Human2 &&
				Board[8] == Human2) ||
				Board[2] == Human2 &&
				Board[4] == Human2 &&
				Board[6] == Human2){
				return 3;
			
		}
	
		// if its a draw
		
		for(int i=0;i<getBoard_Size();i++){
			if(Board[i] != Human1 && Board[i] != Human2){
				return 0;
			}
		}
		
		// if nothing of the above happens continue the game
		
		return 1;
	}
}
